<%-- 
    Document   : dashboard
    Created on : Apr 8, 2010, 12:55:37 AM
    Author     : aaron
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.LinkedList" %>
<%@page import="powerpass.PowerPassSQL" %>
<%@page import="java.sql.SQLException" %>
<%@page import="java.sql.ResultSet" %>
<%@page import="java.lang.Integer" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%!
    public String getPasswordFor(int inAccountid) throws SQLException {
        PowerPassSQL pp = new PowerPassSQL();
        String passSQL = "Select password "
                + "from slccpowerpass.accountpasshistory "
                + "where accountid = " + inAccountid + " "
                + "and enddate = '0000-00-00 00:00:00' "
                + "order by passhistoryid desc "
                + "limit 1";
        ResultSet rs = pp.selectInfo(passSQL);
        rs.next();
        return rs.getString(1);
    }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>DASHBOARD</title>
        <script type="text/javascript" src="js/clipboard.js"></script>
        <script type="text/javascript" src="js/jquery-1.3.2.js" language="javascript"></script>
        <script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js" language="javascript"></script>
        <link rel="stylesheet" href="CSS/jquery-ui-1.7.2.custom.css" type="text/css" />
        <script type="text/javascript" src="zero/ZeroClipboard.js"></script>
        <script type="text/javascript" src="js/clipboard.js"></script>
        <link rel="stylesheet" href="CSS/jquery-ui-1.7.2.custom.css" type="text/css" />
        <link rel="stylesheet" href="table.css" type="text/css" />
        <link rel="stylesheet" href="csstg.css" type="text/css"/>
        <link rel="stylesheet" href="csstg.css" type="text/css">
        <script language="javascript" type="text/javascript">
            function editAccount(index)
            {
                alert(index);
            }
            function validatePassword()
            {
                var password = document.getElementById("password");
                var confirm = document.getElementById("confirmPassword");

                if(confirm.value != password.value)
                {
                    $("<span style='color:red;'>* mismatch</span>").insertAfter(confirm);
                    return false;
                }
                else
                {
                    if(confirm.nextSibling != null)
                    {
                        $(confirm.nextSibling).remove();
                    }
                    return true;
                }

            }
            function createXMLHTTP()
            {
                var xmlhttp = false;
                try{xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
                catch (e){
                    try{xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
                    catch (E){xmlhttp = false;}}
                if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
                    try{xmlhttp = new XMLHttpRequest();}
                    catch (e){xmlhttp = false;}}
                if (!xmlhttp && window.createRequest){
                    try{xmlhttp = window.createRequest();}
                    catch (e){xmlhttp = false;}}
                return xmlhttp;
            }
            function settingsPopup(index)
            {
                var xhr = createXMLHTTP();
                if (xhr)
                {
                    var params = 'selectedIndex=' + index;
                    xhr.onreadystatechange = function ()
                    {
                        if (xhr.readyState == 4)
                        {
                            if (xhr.status == 200)
                            {
                                var returnedObject = xhr.responseText;
                                displayPopup(returnedObject);
                            }
                            else
                            {
                                alert('Unable to fetch Account Data. Status code: ' + xhr.status);
                            }
                        }
                    }
                    xhr.open('POST', 'Service.jsp', true);
                    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    xhr.setRequestHeader("Content-length", params.length);
                    xhr.setRequestHeader("Connection", "close");
                    xhr.setRequestHeader('If-Modified-Since', 'Wed, 15 Jan 1995 01:00:00 GMT');
                    xhr.send(params);
                }
                else
                {
                    alert('Could not create XMLHttpRequest.');
                }
            }

            function displayPopup(returnedObject)
            {
                $(returnedObject).dialog({
                    close: function(event, ui) { $(this).remove(); },
                    resizable: false,
                    width: 500,
                    modal: true,
                    hide: 'fold',
                    show: 'fold',
                    buttons: {
                        Cancel: function () { $(this).dialog("close"); }
                    }
                });
            }

        </script>
    </head>
    <body onload="setTitle_DashBoard();">
        <jsp:useBean id="myAccounts" scope="session" class="accounts.AccountCollectionBean" />
        <table>
            <thead>
                <tr>
                    <th>ACCOUNT NAME</th>
                    <th>DESCRIPTION</th>
                    <th>USERNAME</th>
                    <th>AGE</th>
                    <th>COPY PASSWORD</th>
                    <th>EDIT SETTINGS</th>
                </tr>
            </thead>
            <tbody>
                <%
                    myAccounts.setClientid(Integer.parseInt(session.getAttribute("theClient").toString()));
                    session.setAttribute("myAccounts", myAccounts);
                    LinkedList<accounts.BaseAccount> myList = myAccounts.getAccounts();
                    for (int i = 0; i < myList.size(); i++)
                    {
                        String rowID = "acctid" + myList.get(i).getAccountID();
                        %><tr id="row<%= i%>" >
                            <td id="accountname<%= i%>"> <%=  myList.get(i).getAccountName()%> </td>
                            <td id="description<%= i%>"> <%=  myList.get(i).getAccountDescription()%> </td>
                            <td id="username<%= i%>"> <%=  myList.get(i).getUsername()%> </td>
                            <td id="days<%= i%>"> <%  out.print(i + " days");%> </td>
                            <td id="copycell<%= i%>"><a href="#" id="copier<%= i%>" onclick="">Click to Copy</a></td>
                            <script language="JavaScript">
                                var clip = new ZeroClipboard.Client();
                                clip.setText( '<%= getPasswordFor(myList.get(i).getAccountID()) %>' );
                                clip.glue( 'copier<%= i%>' );
                            </script>
                            <td id="editcell<%= i%>"><a href="#" onclick="settingsPopup('<%= i%>');">Click to Edit</a></td>
                        <tr><%
                    }
                %>
                </tbody>
        </table>
    </body>
</html>